package com.ilove.weather;

import javax.microedition.rms.RecordStore;

public class RMSManager {

	private static RecordStore rs = null;
	static final String REC_STORE = "WEATHER_RMS";

	public static void openRecStore() {
		try {
			rs = RecordStore.openRecordStore(REC_STORE, true);
		} catch (Exception e) {
		}
	}

	public static void closeRecStore() {
		try {
			rs.closeRecordStore();
		} catch (Exception e) {
		}
	}

	public static void deleteRecStore() {
		try {
			RecordStore.deleteRecordStore(REC_STORE);
		} catch (Exception e) {
		}
	}

	public static void writeRecord(String str) {
		byte[] rec = str.getBytes();
		try {
			rs.addRecord(rec, 0, rec.length);
		} catch (Exception e) {
		}
	}

	public static String readRecords() {
		String res = null;
		try {
			byte[] recData = new byte[9];
			int len;
			if (rs.getRecordSize(rs.getNumRecords()) > recData.length) {
				recData = new byte[rs.getRecordSize(rs.getNumRecords())];
			}
			len = rs.getRecord(rs.getNumRecords(), recData, 0);
			res = new String(recData, 0, len);
//			System.out.println(rs.getNumRecords()
//					+ " ------------------------------");
//			System.out.println("Record " + res);
		} catch (Exception e) {
		}
		return res;
	}

	public static void saveUID(String uid) {
		deleteRecStore();
		openRecStore();
		writeRecord(uid);
		closeRecStore();
	}

	public static String readUID() {
		openRecStore();
		String res = readRecords();
		closeRecStore();
		return res;
	}

}
